package list;

import java.util.ArrayList;
@SuppressWarnings({"all"})

public class ListExercise02 {
    public static void main(String[] args) {
        ArrayList arrayList = new ArrayList();
        arrayList.add(new Book("三国",100.0,"罗贯中"));
        arrayList.add(new Book("水浒",110.0,"施耐庵"));
        arrayList.add(new Book("红楼",108.0,"曹雪芹"));
        arrayList.add(new Book("西游",102.0,"吴承恩"));
        for (Object o :arrayList) {
            System.out.println(o);
        }

        //冒泡排序
        sort(arrayList);
        System.out.println("===排序后===");
        for (Object o :arrayList) {
            System.out.println(o);
        }
    }

    public static  void sort(ArrayList arrayList){
        for (int i = 0; i < arrayList.size(); i++) {
            for (int j = 0; j < arrayList.size()-i -1; j++) {
                //取出对象Book
                Book book1 = (Book) arrayList.get(j);
                Book book2 = (Book) arrayList.get(j+1);
                if(book1.getPrice() > book2.getPrice()){//交换
                    arrayList.set(j,book2);
                    arrayList.set(j+1 ,book1);
                }
            }
        }
    }
}